package com.yunhe.xyxxgl.control;

import com.yunhe.xyxxgl.mode.User;
import com.yunhe.xyxxgl.view.User.UserAdd;
import com.yunhe.xyxxgl.view.User.UserDel;
import com.yunhe.xyxxgl.view.User.UserList;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/8 0008.
 */

public class UserController {
    private static Map<String,User> teacherMap = new HashMap<>();
    private static Map<String,User> adminMap = new HashMap<>();
    private static Map<Integer,Map<String,User>> allMap = new HashMap<>();
    static{
        teacherMap.put("teacher",new User("001","teacher","201314","张三",User.TYPE_TEACHER));
        adminMap.put("admin",new User("001","admin","201314","张三",User.TYPE_ADMIN));
        allMap.put(User.TYPE_TEACHER_INT,UserController.teacherMap);
        allMap.put(User.TYPE_ADMIN_INT,UserController.adminMap);
    }
    public static void userControllerRoute(){
        int i = UserList.userListUI();
        switch(i){
            case 1:{
                User user = UserAdd.userAddUI();
                userAdd( user);
                userControllerRoute();
                break;
            }
            case 2:{
                User user = UserDel.userDelUI();
                userRemove( user);
                userControllerRoute();
                break;
            }
            case 3:HomeControllerFactory.creat(UserController.strTypeToInt(LoginController.getCurrUser().getType())).HomeRoute();break;
        }
    }
    public static void userAdd(User user){
        String type = user.getType();
        Map<String,User> userMap = allMap.get(strTypeToInt(type));
        if(null != userMap){
            userMap.put(user.getUserName(),user);
        }

    }
    public static void userRemove(User user){
        String type = user.getType();
        Map<String,User> userMap = allMap.get(strTypeToInt(type));
        if(null != userMap && !user.getUserName().equals(LoginController.getCurrUser().getUserName())){
            userMap.remove(user.getUserName());
        }
    }
    public static int strTypeToInt(String type){
        int rs = 0;
        switch(type){
            case User.TYPE_TEACHER : rs = 1;break;
            case User.TYPE_ADMIN : rs = 2;break;
        }
        return rs;
    }
    public static Map<String, User> getTeacherMap() {
        return teacherMap;
    }

    public static Map<String, User> getAdminMap() {
        return adminMap;
    }

    public static Map<Integer, Map<String, User>> getAllMap() {
        return allMap;
    }
}
